﻿using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.Common;
using yncTech.BusinessObjects;

public partial class Admin_FinLend_FinLend001a : AdminBasePage
{
    decimal? lendId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["lendId"] != null)
        {
            lendId = decimal.Parse(Request.QueryString["lendId"]);
        }
        if (!IsPostBack)
        {
            if (lendId.HasValue)
            {
                FinLend finLend = new FinLend();
                if (finLend.LoadByPrimaryKey(lendId.Value))
                {
                    drpLendType.SelectedValue = finLend.LendType;
                    txtLoanee.Text = finLend.Loanee;
                    txtLeader.Text = finLend.Leader;
                    if (finLend.LendDate.HasValue)
                    { txtLendDate.Value = finLend.LendDate.Value.ToString(); }
                    if (finLend.ReturnDate.HasValue)
                    { txtReturnDate.Value = finLend.ReturnDate.Value.ToString("yyyy-MM-dd"); }
                    txtComments.Text = finLend.Comments;
                    txtLoaneeEmail.Text = finLend.LoaneeEmail;
                    txtLoaneePhone.Text = finLend.LoaneePhone;
                    txtLeaderEmail.Text = finLend.LeaderEmail;

                    TSUserInfoCollection userInfoCollection = new TSUserInfoCollection();
                    userInfoCollection.Query.Where(userInfoCollection.Query.UserName==finLend.Approver);
                    userInfoCollection.Query.Load();
                    drpApprover.SelectedValue = userInfoCollection[0].UserID.Value.ToString();
                    



                    drpThirdUnit.SelectedValue = finLend.ThirdUnitID.Value.ToString();
                    switch (drpLendType.SelectedValue)
                    {
                        case "LendType1":
                            trLendDate.Visible = true;
                            trReturn.Visible = true;
                            trApresent.Visible = false;
                            txtReturnDate.Value = finLend.ReturnDate.Value.ToString();
                            break;
                        case "LendType2":
                            trLendDate.Visible = true;
                            trReturn.Visible = true;
                            trApresent.Visible = false;
                            txtReturnDate.Value = finLend.ReturnDate.Value.ToString();
                            break;
                        case "LendType3":
                            trLendDate.Visible = true;
                            trReturn.Visible = false;
                            trApresent.Visible = false;
                            txtReturnDate.Value = finLend.ReturnDate.Value.ToString();
                            break;
                        case "LendType4":
                            trLendDate.Visible = true;
                            trReturn.Visible = false;
                            trApresent.Visible = false;
                            txtReturnDate.Value = finLend.ReturnDate.Value.ToString();
                            break;
                        case "LendType5":
                            trLendDate.Visible = false;
                            trReturn.Visible = false;
                            trApresent.Visible = true;
                            txtpresentStartTime.Value = finLend.PresentStartTime.Value.ToString();
                            txtpresentEndTime.Value = finLend.PresentEndTime.Value.ToString();
                            break;
                    }
                }

            }
        }
    }

    protected void drpLendType_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        switch (drpLendType.SelectedValue)
        {
            case "LendType1":
                trLendDate.Visible = true;
                trReturn.Visible = true;
                trApresent.Visible = false;
                break;
            case "LendType2":
                trLendDate.Visible = true;
                trReturn.Visible = true;
                trApresent.Visible = false;
                break;
            case "LendType3":
                trLendDate.Visible = true;
                trReturn.Visible = true;
                trApresent.Visible = false;
                break;
            case "LendType4":
                trLendDate.Visible = true;
                trReturn.Visible = true;
                trApresent.Visible = false;
                break;
            case "LendType5":
                trLendDate.Visible = false;
                trReturn.Visible = false;
                trApresent.Visible = true;
                break;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        FinLend finLend = new FinLend();
        if (lendId.HasValue)
        {
            finLend.LoadByPrimaryKey(lendId.Value);
            finLend.UpdatedDttm = DateTime.Now;
            finLend.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
        }
        else
        {
            finLend.CreatedDttm= finLend.UpdatedDttm = DateTime.Now;
            finLend.CreatedBy= finLend.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
            finLend.LendStatus = "LendStatus001";
        }
        finLend.LendType = drpLendType.SelectedValue;
        finLend.Loanee = txtLoanee.Text.Trim();
        finLend.Leader = txtLeader.Text.Trim();
        if (!string.IsNullOrEmpty(txtLendDate.Value.Trim()))
        {
            finLend.LendDate = DateTime.Parse(txtLendDate.Value.Trim());
        }
        if (!string.IsNullOrEmpty(txtReturnDate.Value))
        { finLend.ReturnDate = DateTime.Parse(txtReturnDate.Value); }

        finLend.Comments = txtComments.Text.Trim();
        finLend.LoaneeEmail = txtLoaneeEmail.Text.Trim();
        finLend.LoaneePhone = txtLoaneePhone.Text.Trim();
        finLend.LeaderEmail = txtLeaderEmail.Text.Trim();
        if (!string.IsNullOrEmpty(drpThirdUnit.SelectedValue))
        finLend.ThirdUnitID =decimal.Parse(drpThirdUnit.SelectedValue);

        TSUserInfo userInfo = new TSUserInfo();
        if (userInfo.LoadByPrimaryKey(decimal.Parse(drpApprover.SelectedValue)))
        {
            finLend.Approver = userInfo.UserName;
            finLend.ApproveEmail = userInfo.Email;
        }

        if (!string.IsNullOrEmpty(txtpresentStartTime.Value))
        { finLend.PresentStartTime = DateTime.Parse(txtpresentStartTime.Value); }
        if (!string.IsNullOrEmpty(txtpresentEndTime.Value))
        { finLend.PresentEndTime = DateTime.Parse(txtpresentEndTime.Value); }

        finLend.Save();

        finLend.LoadByPrimaryKey(finLend.LendID.Value);
        finLend.Sn = Business.getLendSN(finLend.LendID.Value,DateTime.Now.Year).ToString();
        finLend.Save();
        WebHelper.CloseDialog(true);
    }
}